from cv2 import cv2 as cv
import numpy as np
import mss  #监听屏幕(比PIL快一些)
import pyautogui as pag #监听鼠标
import time
import threading
import queue
from PIL import Image, ImageDraw, ImageFont
import os
import socket
from WiFi import TX,RX

class Display:

    #-----------------------------#
    #功能：初始化，启动软件
    #-----------------------------#
    def __init__(self):
        self.run = True
        #初始化显示器监听参数
        self.buffer = queue.Queue(2)
        self.width, self.high = pag.size() # 获取当前屏幕的大小
        self.mon = {"top": 0, "left": 0, "width": self.width, "height": self.high}
        self.t1 = threading.Thread(target=self.monitor)
        self.t1.start()

    #-----------------------------#
    #功能：监听主屏幕的线程
    #-----------------------------#
    def monitor(self):
        with mss.mss() as sct:
            #循环捕获主显示器画面
            while self.run:
                img = sct.grab(self.mon)    #捕获画面
                img = cv.cvtColor(np.array(img),cv.COLOR_BGRA2BGR)#色彩空间转换
                if self.buffer.full():
                    try:    self.buffer.get(timeout=0.005)
                    except: pass
                self.buffer.put(img,False)

ip = input('请输入本机IP：')

sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
sock.bind((ip,5600))

ip = input('请输入接收端ip：')
port = input('接收端口：')
tx = TX.Transmitter(sock=sock,recver_ip=ip,port=int(port))

dispaly = Display()

while True:
    img = dispaly.buffer.get()
    tx.sendimg(img)
    #cv.imshow('img',img)
    #cv.waitKey(1)
